<#macro scriptMacro>
  <script>
    layui.use(['form', 'table'], function () {
      var $ = layui.jquery,
          form = layui.form,
          table = layui.table;

      table.render({
        elem: '#articleListTable',
        request: {
          pageName: 'current',
          limitName: 'size'
        },
        where: {
          title: $('input[name=title]').val(),
          status: $('select[name=status]').val()
        },
        url: '${request.contextPath}/article/pageByQuery',
        toolbar: '#articleTopToolBar',
        defaultToolbar: ['filter'],
        cols: [[
          {field: 'title', width: 150, title: '文章标题'},
          {
            field: 'statusDesc',
            width: 100,
            title: '状态',
            templet: '<div><span class="layui-badge layui-bg-{{ d.status }}">{{ d.statusDesc }}</span></div>'
          },
          {field: 'summary', width: 250, title: '概要'},
          {field: 'isIndex', width: 120, title: '首页显示', templet: '<div>{{ d.isIndex?"是":"否" }}</div>'},
          {field: 'isTop', width: 80, title: '置顶', templet: '<div>{{ d.isTop?"是":"否" }}</div>'},
          {
            field: 'viewCount',
            width: 100,
            title: '浏览量',
            sort: true,
            templet: '<div>{{ d.viewCount?d.viewCount:0 }}</div>'
          },
          {
            field: 'thumbCount',
            width: 100,
            title: '点赞量',
            sort: true,
            templet: '<div>{{ d.thumbCount?d.thumbCount:0 }}</div>'
          },
          {
            field: 'needPassword',
            width: 100,
            title: '加密',
            templet: '<div>{{ d.needPassword?"是":"否" }}</div>'
          },
          {
            field: 'isOriginal',
            width: 120,
            title: '原创',
            sort: true,
            templet: '#originTpl'
          },
          {
            title: '创建时间',
            minWidth: 150,
            sort: true,
            templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'
          },
          {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        page: true,
        skin: 'line',
        parseData: function (res) {
          var page = res.data;
          if (page) {
            currentPage = page.current;
            return {
              "code": res.code,
              "msg": res.msg,
              "count": page.total,
              "data": page.records
            }
          } else {
            return {
              "code": res.code,
              "msg": res.msg
            }
          }
        },
      });

      table.on('tool(articleListTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'delete') {
          layer.confirm('真的删除行么', function (index) {
            $.post("${request.contextPath}/article/delete", {
              id: data.id
            }, function (result) {
              layMsg(result.msg);
              if (result.success) {
                layer.close(index);
                reloadTable();
              }
            })
          });
        } else if (obj.event === 'physicalDelete') {
          layer.confirm('物理删除将删除数据库记录，确定要删除吗？', function (index) {
            $.post("${request.contextPath}/article/physicalDelete", {
              id: data.id
            }, function (result) {
              layMsg(result.msg);
              if (result.success) {
                layer.close(index);
                reloadTable();
              }
            })
          });
        }
      });

      $("#search-btn").on('click', function () {
        reloadTable(1);
      })
    });

    var currentPage = 1;

    function reloadTable(page) {
      layui.use('table', function () {
        var $ = layui.jquery, table = layui.table;
        //执行搜索重载
        table.reload('articleListTable', {
          page: {
            curr: page ? page : currentPage
          },
          where: {
            title: $('input[name=title]').val(),
            status: $('select[name=status]').val()
          }
        }, true);
      })
    }

  </script>
</#macro>
<@zlt.page title="文章列表" scriptMacro=scriptMacro>
  <body>
  <div class="layuimini-container">
    <div class="layuimini-main">
      <fieldset class="table-search-fieldset">
        <legend>搜索信息</legend>
        <div class="layui-form" style="margin: 10px 10px 10px 10px">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">标题</label>
              <div class="layui-input-inline">
                <input type="text" name="title" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">状态</label>
              <div class="layui-input-inline">
                <select name="status" lay-filter="">
                  <option value="">请选择状态</option>
                    <#list articleStatus as s>
                      <option value="${s.value}">${s.desc}</option>
                    </#list>
                </select>
              </div>
            </div>
            <div class="layui-inline">
              <button type="button" id="search-btn" class="layui-btn layui-btn-primary loading-btn"
                      data-reset-time="1000"><i class="layui-icon"></i> 搜 索
              </button>
            </div>
          </div>
        </div>
      </fieldset>

      <script type="text/html" id="articleTopToolBar">
        <div class="layui-btn-container">
          <a class="layui-btn layui-btn-normal layui-btn-sm" href="javascript:;"
                  layuimini-content-href="/article/save/" data-title="新建文章"> 新建文章</a>
        </div>
      </script>

      <table class="layui-hide" id="articleListTable" lay-filter="articleListTableFilter"></table>

      <script type="text/html" id="currentTableBar">
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit"
                href="javascript:;" layuimini-content-href="/article/save/{{d.id}}" data-title="修改文章">编辑</a>
        {{# if(!d.deleted){ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
        {{# } else if(d.deleted){ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="physicalDelete">物理删除</a>
        {{# } }}
      </script>
      <script type="text/html" id="originTpl">
        {{# if(d.isOriginal){ }}
        <span class="layui-badge layui-bg-green">是</span>
        {{# } else { }}
        <span class="layui-badge">否</span>&nbsp;
        <a class="layui-btn layui-btn-warm layui-btn-xxs" href="{{ d.originalUrl }}" target="_blank">原地址</a>
        {{# } }}
      </script>
    </div>
  </div>
  </body>
</@zlt.page>